(function (vc) {

    vc.extends({
        data: {
            addCommunityRecommendInfo: {
                crId: '',
                communityId:'',
                communityName: '',
                shopName: '未知',
                shopId: '9999',
                recommendType: 'OUT',
                logo: '',
                prodName: '',
                price: '',
                url: '',

            }
        },
        _initMethod: function () {

        },
        _initEvent: function () {
            vc.on('addCommunityRecommend', 'openAddCommunityRecommendModal', function (_param) {
                $that.addCommunityRecommendInfo.communityId = _param.communityId;
                $that.addCommunityRecommendInfo.communityName = _param.communityName;
                $('#addCommunityRecommendModel').modal('show');
            });
            vc.on("addCommunityRecommend", "notifyUploadCoverImage", function (_param) {
                if (_param) {
                    $that.addCommunityRecommendInfo.logo = _param[0].url
                } else {
                    $that.addCommunityRecommendInfo.logo = '';
                }
            });
        },
        methods: {
            addCommunityRecommendValidate() {
                return vc.validate.validate({
                    addCommunityRecommendInfo: $that.addCommunityRecommendInfo
                }, {
                    'addCommunityRecommendInfo.logo': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "推荐logo不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "512",
                            errInfo: "推荐logo不能超过512"
                        },
                    ],
                    'addCommunityRecommendInfo.prodName': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "商品名称不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "256",
                            errInfo: "商品名称不能超过256"
                        },
                    ],
                    'addCommunityRecommendInfo.price': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "价格不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "10",
                            errInfo: "价格不能超过10"
                        },
                    ],
                    'addCommunityRecommendInfo.url': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "访问地址不能为空"
                        },
                    ],
                });
            },
            saveCommunityRecommend: function () {
                if (!$that.addCommunityRecommendValidate()) {
                    vc.toast(vc.validate.errInfo);
                    return;
                }
                vc.http.apiPost(
                    '/recommend.saveCommunityRecommend',
                    JSON.stringify($that.addCommunityRecommendInfo),
                    {
                        emulateJSON: true
                    },
                    function (json, res) {
                        //vm.menus = vm.refreshMenuActive(JSON.parse(json),0);
                        let _json = JSON.parse(json);
                        if (_json.code == 0) {
                            //关闭model
                            $('#addCommunityRecommendModel').modal('hide');
                            $that.clearAddCommunityRecommendInfo();
                            vc.emit('communityRecommend', 'listCommunityRecommend', {});

                            return;
                        }
                        vc.toast(_json.msg);
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                        vc.toast(errInfo);
                    });
            },
            clearAddCommunityRecommendInfo: function () {
                $that.addCommunityRecommendInfo = {
                    crId: '',
                    communityId:'',
                    communityName: '',
                    shopName: '未知',
                    shopId: '9999',
                    recommendType: 'OUT',
                    logo: '',
                    prodName: '',
                    price: '',
                    url: '',

                };
            }
        }
    });

})(window.vc);
